package com.sfh.cpc.com.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.ImageLoaderEngine;
import com.sfh.cpc.com.R;
import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.server.INoteServer;
import com.sfh.lib.AppCacheManager;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 功能描述: 用户信息
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/7
 */
public class WidgetMassUserInfo extends RelativeLayout implements View.OnClickListener {

    CircleImageView ivHead;
    TextView tvName;
    ImageView ivSex;
    VIPAgeUserView tvAddress;
    WidgetCharmWealth wgWealth;
    WidgetCharmWealth wgCharm;
    ImageView ivVIP;

    long userId;

    public WidgetMassUserInfo(Context context) {
        super(context);
        init();
    }

    public WidgetMassUserInfo(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WidgetMassUserInfo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        inflate(this.getContext(), R.layout.widget_mass_userinfo, this);
        this.ivHead = this.find(R.id.ivHeadr);
        this.tvName = this.find(R.id.tvName);
        this.ivSex = this.find(R.id.ivSex);
        this.tvAddress = this.find(R.id.tvAddress);
        this.ivVIP = this.find(R.id.ivVIP);
        this.wgWealth = this.find(R.id.wgWealth);
        this.wgCharm = this.find(R.id.wgCharm);

        this.wgWealth.setStyleWealth("0");
        this.wgCharm.setStyleCharm("0");

        this.ivHead.setOnClickListener(this);

        this.wgWealth.setOnClickListener(this);
        this.wgWealth.setOnClickListener(this);
    }

    private <T> T find(int r) {
        return (T) findViewById(r);
    }

    public void showData(EUser user) {

        if (user == null) {
            return;
        }
        this.userId = user.getUserId();
        this.tvName.setText(user.getUserName());
        this.ivSex.setImageResource(TextUtils.equals("1", user.getUserSex()) ? R.drawable.tag_male : R.drawable.tag_female);
        this.tvAddress.showVIP(user);

        this.ivVIP.setVisibility(user.isVip()?VISIBLE:GONE);

        if (user.getUserWealth() <= 0L) {
            this.wgWealth.setVisibility(GONE);
        } else {
            this.wgWealth.setVisibility(VISIBLE);
            this.wgWealth.setStyleWealth(String.valueOf(user.getUserWealth()));
        }

        if (user.getUserCharm() <= 0L) {
            this.wgCharm.setVisibility(GONE);
        } else {
            this.wgCharm.setVisibility(VISIBLE);
            this.wgCharm.setStyleCharm(String.valueOf(user.getUserCharm()));
        }


        if (TextUtils.isEmpty(user.getUserUrl())) {
            this.ivHead.setImageResource(R.drawable.wait_head_second);
        } else {
            new ImageLoaderEngine(this.ivHead, BaseAppContent.getImageUrl(user.getUserUrl())).resizeDimen(R.dimen.px_137, R.dimen.px_137).placeholder(R.drawable.wait_head_second).error(R.drawable.wait_head_second).centerCrop().into(this.ivHead);
        }

    }


    @Override
    public void onClick(View v) {
        if (v == ivHead){
            INoteServer userServer = AppCacheManager.getCache(INoteServer.class.getSimpleName(), INoteServer.class);
            if (userServer != null) {
                userServer.openUserNote(getContext(), this.userId);
            }
        }else{

        }
    }
}
